<?php

namespace Biz\model\activity;

use Doraemon\model\Admin\BeerUsUser;


class BizProjectPrepareModel extends BizProjectBaseModel
{
    const _TABLE_NAME = 'biz_project_pre';

    const CHANGE_CONTENT_1 = 1; //1、均不需要
    const CHANGE_CONTENT_2 = 2; //2、策划方案
    const CHANGE_CONTENT_3 = 3; //3、媒介报价
    const CHANGE_CONTENT_4 = 4; //4、策划方案与媒介报价

    static array $foldKey = ['responsible','customer','project','process','contract','costs'];

    use BizProjectTrait;


    public static function getAdmList(array $condition): array
    {
        $response = [];
        $list = self::dataSelect( self::statusCondition() + $condition);
        $ids = $list ? array_column($list, 'id') : [];
        $responsible = BizProjectResponsibilityModel::findAdminByProjectId($ids, BizProjectResponsibilityModel::RESPONSIBILITY_MODULE_PREPARE);
        $admin_ids = BizProjectResponsibilityModel::afterFindAdmin();
        BeerUsUser::getNameByUsername($admin_ids);
        $project_category_map = BizProjectPRelationIdsModel::getPreRelationIds($ids);
        if ($list) {
            $category = BizProjectCategoryModel::adminNameLevelOptions();
            foreach ($list as $v) {
                $tempResponsible = $responsible[$v['id']] ?? [];
                $v['created_at_date'] = $v['created_at'];
                $v['seller'] = BeerUsUser::afterGetRowsMap($tempResponsible['seller'] ?? []);
                $v['planner'] = BeerUsUser::afterGetRowsMap($tempResponsible['planner'] ?? []);
                $v['medium'] = BeerUsUser::afterGetRowsMap($tempResponsible['medium'] ?? []);
                $v['project_category'] = $project_category_map[$v['id']] ?? [];
                $temp = [];
                foreach ($v['project_category'] as $categoryId) {
                    $temp[] = $category[$categoryId];
                }
                $v['project_category'] = $temp;
                $v['project_money_format'] = number_format(floatval($v['project_money']), 2);
                $v['project_time'] = date("Y-m-d", strtotime($v['project_start_date'])) . "~" . date("Y-m-d", strtotime($v['project_end_date']));
                $v['responsible'] = $tempResponsible;
                $response[] = $v;
            }
        }
        return $response;
    }

    public static function getAdmInfo($id): array
    {
        $info = $id ? self::get("*", ['id' => $id]) : [];


        return $info ? self::formatAdmInfo($info) : [];
    }

    public static function formatAdmInfo($info): array
    {
        $recordBudget = $recordReason = '';
        if ($info['id']) {
            $infoRecord = BizProjectPrepareModelRecord::getListByCondition(['pre_id' => $info['id']]);
            foreach ($infoRecord as $value) {
                if ($value['type'] == BizProjectPrepareModelRecord::TYPE_1) {
                    $recordReason .= '历史记录：' . $value['record'] . '<br>' . '填写人：' . $value['admin'] . '<br>' . '日期：' . $value['created_at'] . '<br>' . '<br>';
                } else {
                    $recordBudget .= '历史记录：' . $value['record'] . '<br>' . '填写人：' . $value['admin'] . '<br>' . '日期：' . $value['created_at'] . '<br>' . '<br>';
                }
            }
        }

        $format = $info;
        $format['budget'] = $recordBudget;
        $format['reason'] = $recordReason;
        $format['project_category'] = BizProjectPRelationIdsModel::getPreRelationIds($info['id']);
        $format['responsible'] = BizProjectResponsibilityModel::findAdminByProjectId($info['id'], BizProjectResponsibilityModel::RESPONSIBILITY_MODULE_PREPARE);
        return $format;
    }



}
